﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;

namespace MDP.WebOpenPortal.Controllers
{
    //[SSOAuthorizeAttribute]
    public partial class BaseController : Controller
    {
        protected LoginInfo UserInfo
        {
            get
            {
                string userName = string.Empty;
                if (System.Web.HttpContext.Current.User != null
                    && System.Web.HttpContext.Current.User.Identity != null
                    && !string.IsNullOrEmpty(System.Web.HttpContext.Current.User.Identity.Name))
                {
                    userName = System.Web.HttpContext.Current.User.Identity.Name;
                }
                return new LoginInfo() { UserName = userName };
            }
        }

        protected UrlRoute CheckUrlRoute(string entityName)
        {
            try
            {
                string id = Request.Params["id"];//collection["id"];
                if (string.IsNullOrEmpty(id))
                {
                    throw new ArgumentNullException("id 参数为空");
                }
                UrlRouteBLL urlDAL = new UrlRouteBLL(null);
                var urlRouteInfo = urlDAL.GetInfoById(id);
                if (urlRouteInfo == null)
                {
                    throw new InvalidOperationException(string.Format("参数 id={0} 不存在，检查是否过期。", id));
                }
                if (!urlRouteInfo.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase))
                {
                    throw new ArgumentException(string.Format("参数 id={0} 不存在，检查是否过期。", id));
                }
                var userInfo = this.UserInfo;
                if (!string.IsNullOrEmpty(urlRouteInfo.UserID))
                {
                    if (!urlRouteInfo.UserID.Equals(userInfo.UserName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        throw new ArgumentException(string.Format("参数 id={0} 对应的当前用户 {1} 错误。", id, userInfo.UserName));
                    }
                }
                return urlRouteInfo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}